package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageReturnListActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageReturnOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageReturnListBean;
import com.inossem.warehousestorage_inossem.utils.QRCodeEditTextWatcher;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class ReturnOutStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStorageReturnListBean.BodyBean> mReturnOutStorageList;
    private LayoutInflater inflater;
    private OutStorageReturnListActivity.OutStorageReturnListener mOutStorageReturnListener;
    private String mPurchaseNumber;

    public ReturnOutStorageListAdapter(Context context, List<OutStorageReturnListBean.BodyBean> returnOutStorageList, String purchaseNumber, OutStorageReturnListActivity.OutStorageReturnListener outStorageReturnListener) {
        mContext = context;
        mReturnOutStorageList = returnOutStorageList;
        mOutStorageReturnListener = outStorageReturnListener;
        inflater = LayoutInflater.from(context);
        mPurchaseNumber = purchaseNumber;
    }

    @Override
    public int getCount() {
        return mReturnOutStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReturnOutStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText purchaseNumber = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            purchaseNumber.setText(mPurchaseNumber);
            purchaseNumber.setHint(mContext.getResources().getString(R.string.activity_out_storage_return_list_please_enter_the_purchase_order_number));
            purchaseNumber.addTextChangedListener(new QRCodeEditTextWatcher(purchaseNumber) {
                @Override
                public void onScan(String text) {
                    Utils.closeSoftKeyboard(mContext, purchaseNumber);
                    mOutStorageReturnListener.onNoDoubleClickListener(text);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageReturnListener.onNoDoubleClickListener(purchaseNumber.getText().toString());
                }
            });
        } else {
            OutStorageReturnListBean.BodyBean returnOutInstorageItem = mReturnOutStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_return_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, returnOutInstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.purchaseNumber = (TextView) view.findViewById(R.id.purchaseNumber);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, OutStorageReturnListBean.BodyBean saleOutStorageItem) {
        viewHolder.purchaseNumber.setText(saleOutStorageItem.getRefer_receipt_code());
        viewHolder.supplier.setText(saleOutStorageItem.getReceive_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStorageReturnOverviewActivity.class);
                intent.putExtra(OutStorageExtra.OUT_STORAGE_PURCHASE_NUMBER, viewHolder.purchaseNumber.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView purchaseNumber, supplier;
    }
}
